<?php

    if (!defined("EXPONENT")) exit("");
    $sermon = null;		
    if (isset($_POST['id'])) {
        $sermon = $db->selectObject('documentsmodule_document', 'id='.$_POST['id']);
        if ($sermon != null) {
            $loc = unserialize($sermon->location_data);
        } 
    }
    else
    {
        $sermon->rank = $db->max('documentsmodule_document', 'rank', 'location_data', "location_data='".serialize($loc)."'");
        if ($sermon->rank == null) {
            $sermon->rank = 0;
        } else {
            $sermon->rank += 1;
        }
    }

    if (exponent_permissions_check("create",$loc) || exponent_permissions_check("administrate",$loc)) {	
        //$config = $db->selectObject('sermonmodule_sermon',"location_data='".serialize($loc)."'");

        $sermon= documentsmodule_document::update($_POST, $sermon);
        $sermon->location_data = serialize($loc);
        //print_r($sermon);
        //die();
        if (isset($sermon->id)) {
            // calculate all song length
            $db->updateObject($sermon,"documentsmodule_document");
        } else {

            $db->insertObject($sermon,"documentsmodule_document");
        }		

        exponent_flow_redirect();
    } else {
        echo SITE_403_HTML;
    }


?>
